﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using System.Xml;
using Microsoft.SharePoint;

namespace KbWorksBlogMigrationtool
{
    public partial class MigrationForm : Form
    {
        string url = string.Empty;
        //            string beginurl = string.Empty;
        string newurl = string.Empty;
        //            string endUrl = string.Empty;
        string oldUrl = string.Empty;
        Logging log = new Logging();
        AppSettingsReader conf = new AppSettingsReader();
        ArrayList mySites = new ArrayList();
        ArrayList allSites = new ArrayList();
        public MigrationForm()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {

            XmlNodeList webs = null;
            //string url = string.Empty;
            ////            string beginurl = string.Empty;
            //string newurl = string.Empty;
            ////            string endUrl = string.Empty;
            //string oldUrl = string.Empty;
            oldUrl = txt2007BlogURL.Text;
            url = txt2010BlogURL.Text;
            newurl = url;
            ListsProxy2007 listProxy = new ListsProxy2007(txt2007BlogURL.Text);

            XmlNode lists = null;
            try
            {
                lists = listProxy.GetLists();
            }
            catch (Exception ex)
            {
                log.append("problem getting Lists" + ex.ToString(), 4, "Error");
            }
            Boolean Posts = false;
            //Continue with next web if unable to get the lists of the current web
            if (lists != null)
            {
                XmlNodeList list = null;
                list = Util.RunXPathQuery(lists, "//sp:List");
                int listcount = 0;
                Dictionary<string, int> bloglists = new Dictionary<string, int>();
                while (listcount < list.Count)// in lists.Tables[0].Rows)
                {
                    bloglists.Add(list[listcount].Attributes["Title"].Value, listcount);
                    listcount++;
                }
                int value = 0;
                if (bloglists.ContainsKey("Categories"))
                {

                    value = Convert.ToInt32(bloglists["Categories"]);
                    FillLists("Categories", value, list);
                }
                if (bloglists.ContainsKey("Photos"))
                {
                    value = bloglists["Photos"];
                    FillLists("Photos", value, list);
                  
                }
                if (bloglists.ContainsKey("Posts"))
                {
                    value = bloglists["Posts"];
                    FillLists("Posts", value, list);
                    //need approval
                    using (SPSite site = new SPSite(url))
                    {
                        site.AllowUnsafeUpdates = true;
                        using (SPWeb web = site.OpenWeb())
                        {//makes connection and opens the site to allow modification
                            web.AllowUnsafeUpdates = true;
                            SPList newlist = web.Lists["Posts"];
                            Approveitems.PublishListItem(newlist);
                        };
                    };
                }
                if (bloglists.ContainsKey("Comments"))
                {
                    value = bloglists["Comments"];
                    FillLists("Comments", value, list);
                    using (SPSite site = new SPSite(url))
                    {
                        site.AllowUnsafeUpdates = true;
                        using (SPWeb web = site.OpenWeb())
                        {//makes connection and opens the site to allow modification
                            web.AllowUnsafeUpdates = true;
                            SPList newlist = web.Lists["Comments"];
                            Approveitems.PublishListItem(newlist);
                        };
                    };
                }
                if (bloglists.ContainsKey("Links"))
                {
                    value = bloglists["Links"];
                    FillLists("Links", value, list);
                }

                //  string newListName = string.Empty;
                //string listName = list[listcount].Attributes["Title"].Value; //TODO: Is column 3 the listname for all lists?

                //                                }



            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            MessageBox.Show("Ready");
        }
        public void FillLists(string listName, int listid, XmlNodeList list2007)
        {
            string newListName = listName;

            ListItemsProxy2007 listItemsProxy = new ListItemsProxy2007(oldUrl, listName);
            XmlNode items = null;
            XmlNodeList listItems = null;
            try
            {
                items = listItemsProxy.GetListItems();
                listItems = Util.RunXPathQuery(items, "//z:row");
                int Rows = 0;
                log.append("Count old rows= " + listName + "  " + listItems.Count, 4, "List");
                //this.Update();
                //this.Refresh();
                while (Rows < listItems.Count)
                {
                    // create row in list
                    ListItemsProxy2010 newItems = new ListItemsProxy2010();
                    newItems.CreateItems(listItems[Rows],
                        newurl,
                        newListName, Convert.ToInt32(list2007[listid].Attributes["BaseType"].Value), oldUrl);

                    Rows++;
                }

            }
            catch (Exception ex)
            {
                log.append("Somthing going wrong with the items" + ex.ToString(), 4, "Error");
                //throw new ArgumentException("Somthing going wrong with the items", ex.ToString());
            }
            //Continue with next list if unable to get the list item of the current list


        }

        private void MigrationTool_Load(object sender, EventArgs e)
        {
            //  lblLog.Text = "Location Of the log is " + Convert.ToString(conf.GetValue("logdirectory", typeof(string)));

        }

        private void txt2007BlogURL_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt2010BlogURL_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


    }
}